﻿Imports System.Printing

Public Class DumpView

    Private mVm As DumpViewModel

    Private Sub DumpView_Initialized(sender As Object, e As System.EventArgs) Handles Me.Initialized
        'add handler for the PrintCommand so we can override and allow Print
        Dim PrintBinding As New CommandBinding(ApplicationCommands.Print, _
           New ExecutedRoutedEventHandler(AddressOf OnPrintCommandExecuted))
        Dim PrintCmdKeyBinding As New KeyBinding(ApplicationCommands.Print, Key.P, ModifierKeys.Control)
        Me.CommandBindings.Add(PrintBinding)
        Me.InputBindings.Add(PrintCmdKeyBinding)
    End Sub

    Public Sub New(ByVal snaps As List(Of HeapSnapshot))

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        mVm = New DumpViewModel(snaps)
        Me.DataContext = mVm

    End Sub

    Private Sub OnPrintCommandExecuted(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
        PrintScreen()
        e.Handled = True
    End Sub

    Private Sub PrintScreen()
        Dim area As PrintDocumentImageableArea = Nothing
        Dim writer As Xps.XpsDocumentWriter = PrintQueue.CreateXpsDocumentWriter(area)
        If writer IsNot Nothing Then

            'set up margins
            'Dim leftMargin As Double = 96
            'Dim rightMargin As Double = 96
            'Dim topMargin As Double = 96
            'Dim bottomMargin As Double = 96
            'Me.Margin = New Thickness(leftMargin, topMargin, rightMargin, bottomMargin)

            're-set size of visual to fit print area
            'Dim outputSize As New Size(area.MediaSizeWidth, area.MediaSizeHeight)
            'Me.Measure(outputSize)
            'Me.Arrange(New Rect(outputSize))

            'Dim outputSize As New Size(area.MediaSizeHeight, area.MediaSizeWidth)
            'Me.Measure(outputSize)
            'Me.Arrange(New Rect(25, 25, area.MediaSizeHeight, area.MediaSizeWidth))

            Dim ticket As New PrintTicket
            ticket.PageOrientation = PageOrientation.Landscape
            'ticket.PageScalingFactor = 75

            'Transform the window to scale it down and position it to fit on the printed page
            'Dim transG As New TransformGroup
            'transG.Children.Add(New ScaleTransform(0.75, 0.75))
            'transG.Children.Add(New TranslateTransform(25, 25))
            'Me.VisualTransform = transG

            writer.Write(Me, ticket)

        End If
        'Rescale and position the image for the screen....this should be transparent to the user.
        Dim tg As New TransformGroup
        tg.Children.Add(New ScaleTransform(1, 1))
        tg.Children.Add(New TranslateTransform(0, 0))
        Me.VisualTransform = tg
    End Sub

    Private Sub MenuPrint_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)

    End Sub

End Class
